<?php

namespace app\distribution\controller;

use app\common\controller\Adminbase;
use app\job\UpdateGoodsSharePrice;
use app\model\DistributionSetting;
use think\Queue;

class Setting extends Adminbase
{

    public function form()
    {
        $uuid = 'setting';
        $info = DistributionSetting::where('uuid', $uuid)->find();
        if (empty($info)) {
            $info = [
                'uuid'  => 'setting',
                'name'  => '分销设置',
                'type'  => 0,
                'value' => json_encode([
                    ['name' => '白银'],
                    ['name' => '黄金'],
                ]),
            ];
            DistributionSetting::create($info);
        } else {
            $info['level'] = json_decode($info['value'], true);
        }
        $ratio = $info['level'][0]['ratio'] ?? 0;
        if ($this->request->isPost()) {
            $data           = $this->request->post();

			// 参数合法性校验
			foreach ($data['level'] as $item) {
				if (isset($item['ratio']) && $item['ratio'] > 100) {
					return json(['code' => -1, 'msg' => '佣金收益比例不能大于100%']);
				}
				if (isset($item['ratio']) && $item['ratio'] < 0) {
					return json(['code' => -1, 'msg' => '佣金收益比例不能小于0%']);
				}
				if (isset($item['team_ratio']) && $item['team_ratio'] > 100) {
					return json(['code' => -1, 'msg' => '团队收益比例不能大于100%']);
				}
				if (isset($item['team_ratio']) && $item['team_ratio'] < 0) {
					return json(['code' => -1, 'msg' => '团队收益比例不能小于0%']);
				}
				if (isset($item['day']) && $item['day'] < 0) {
					return json(['code' => -1, 'msg' => '有效时间天数不能小于0']);
				}
				if (isset($item['invite_count']) && $item['invite_count'] < 0) {
					return json(['code' => -1, 'msg' => '推客数量不能小于0']);
				}
				if (isset($item['order_count']) && $item['order_count'] < 0) {
					return json(['code' => -1, 'msg' => '购买订单数量不能小于0']);
				}
				if (isset($item['ratio']) && isset($item['team_ratio']) && ($item['ratio'] + $item['team_ratio'] > 100)) {
					return json(['code' => -1, 'msg' => '总收益比例不能大于100%']);
				}
			}

//            $data['income'] = $data['income'] > 0 ? ($data['income'] * 100) : 0;
            $data['value']  = json_encode($data['level'], JSON_UNESCAPED_UNICODE);
            //判断推客分享赚/自买返比例是否改变
            $update_ratio = $data['level'][0]['ratio'] ?? 0;
            if ($ratio != $update_ratio) {
                Queue::push(UpdateGoodsSharePrice::class, ['ratio' => intval($update_ratio)], 'tuike');
            }

            unset($data['level']);
            DistributionSetting::where('uuid', $uuid)->update($data);
            return json(['code' => 0, 'msg' => '操作成功']);
        }

        $type = [
            0 => '关闭',
            1 => '一级',
        ];
        $this->assign([
            'type'           => $type,
            'info'           => $info,
            'income'         => $info['income'] > 0 ? ($info['income'] / 100) : 0,
            'task_condition' => $info['task_condition'] ?? 0,
        ]);
        return $this->fetch('setting/form');
    }

}
